using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using Acms.SqlDal;

namespace Acms.Bll
{
    public class UserPostVote : BaseItem
    {
        protected UserPostVote() : base() { }
        protected UserPostVote(Hashtable table) : base(table) { }
        protected UserPostVote(Hashtable table, List<UserPostVote> collection) : base(table, collection) { }
        
        public static UserPostVote CreateInstance()
        {
            return new UserPostVote();
        }

        public static UserPostVote CreateInstance(int userId, int postId)
        {
            UserPostVote tmp = new UserPostVote();
            tmp.userId = userId;
            tmp.postId = postId;
            return tmp;
        }

        private static UserPostVote Get(Hashtable table)
        {
            if (table != null)
            {
                return new UserPostVote(table);
            }
            else
            {
                return null;
            }
        }

        private static List<UserPostVote> Get(Hashtable[] tables)
        {
            if (tables != null)
            {
                List<UserPostVote> tmp = new List<UserPostVote>();
                foreach (Hashtable table in tables)
                {
                    tmp.Add(new UserPostVote(table, tmp));
                }
                return tmp;
            }
            else
            {
                return null;
            }
        }

        public static UserPostVote GetByUserIdAndPostId(int userId, int postId)
        {
            UserPostVoteDal dal = new UserPostVoteDal();
            return Get(dal.GetByUserIdAndPostId(userId, postId));
        }

        public new void Update()
        {
            base.Update();
        }

        protected override void SaveChanges(ref object concurencyId, Base dal)
        {
            if(IsNewRecord)
            {
                created = DateTime.Now;
            }
            ((UserPostVoteDal)dal).Update(ref id, userId, postId, score, created, ref concurencyId);
        }

        protected override void Load(Hashtable table)
        {
            id = (int)table[UserPostVoteDal.Fields.Id];
            userId = (int)table[UserPostVoteDal.Fields.UserId];
            postId = (int)table[UserPostVoteDal.Fields.PostId];
            score = (decimal)table[UserPostVoteDal.Fields.Score];
            created = (DateTime)table[UserPostVoteDal.Fields.Created];
        }

        private int id;
        public int Id
        {
            get { return id; }
        }
	

        private int userId;
        public int UserId
        {
            get { return userId; }
        }
	
        private int postId;
    	public int PostId
	    {
	        get { return postId;}
	    }

        private decimal score;
        public decimal Score
        {
            get { return score;}
            set { score = value; }
        }

        private DateTime created;
        public DateTime Created
        {
            get { return created; }
        }
	
        protected override Acms.SqlDal.Base CreateDal()
        {
            return new UserPostVoteDal();
        }
    }
}
